home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16135 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  92 lines

  1. Path: news.nask.org.pl!usenet
  2. From: piotrpar@blue.maloka.waw.pl (Piotr Parlewicz)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Can somebody explain me how to do this in C++
  5. Date: Tue, 09 Apr 1996 18:45:40 GMT
  6. Organization: Research and Academic Computer Network
  7. Message-ID: <4keb0h$d5e@bilbo.nask.org.pl>
  8. References: <4hkb2o$ko3@mo6.rc.tudelft.nl>
  9. NNTP-Posting-Host: s114.maloka.waw.pl
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Ejo Schrama <schrama@geo.tudelft.nl> wrote:
  13.  
  14. >Suppose you've got a base class containing protected data and several 
  15. >public methods among which there are some virtual ones. This base class
  16. >is inhereted to "lets call them" subclasses in which the virtual methods
  17. >are implemented. All subclasses are defined during the initialization of a
  18. >problem, however during the execution of a program "where all work is
  19. >executed" you only want to deal with a linked list of base class objects
  20. >which are accessed via a while loop. The code looks as follows:
  21.  
  22. [...]
  23.  
  24. >Any attempt I do to declare
  25.  
  26. >  Cparam_inhereted_method *localpointer2 = localpointer; 
  27.  
  28. >will always fail since:
  29. [...]
  30.  
  31.  
  32. >so that I can never execute
  33.  
  34. >  localpointer2->execute_routine();
  35.  
  36. >The only alternative seems to introduce execute_routine to the Cparam
  37. >base class (which I would like to avoid). How can I avoid this?
  38.  
  39. Hi, if I understood your intentions correctly, it seems that all your
  40. code was missing was an explicit type cast to the derived type.
  41. Here is a working (BC4.51) example:
  42.  
  43.  
  44. #include <stdio.h>
  45.  
  46. class CBase {
  47. protected:
  48. char      d ;
  49. public:
  50. void m1( ) {
  51.     printf("m1 in base\n") ;
  52.     }
  53.  
  54. virtual void m2( ) {
  55.     printf("m2 in base\n");
  56.     }
  57. }  ;
  58.  
  59. class CDer : CBase {
  60. protected:
  61. char e;
  62. public:
  63. void m3( ) {
  64.     printf("M3 in Derived\n");
  65.     }
  66.  
  67. void m2()  {
  68.     printf("M2 in Derived...\n");
  69.     }
  70.  
  71. };
  72.  
  73.  
  74.  
  75. int main( int argc, char **argv ) {
  76.  
  77. class CDer cd ; // cd will be object of derived type
  78. CBase *cb = (CBase*)&cd ; // cb will be generic base class pointer
  79. CDer *cdp = (CDer *)cb ; // now do your cast to more complex (derived)
  80. type
  81. cdp->m2(); // check if virtual function knows we are really of derived
  82. type
  83. cdp->m3(); // check if function existing only in derived works
  84. }
  85.  
  86.  
  87. Hope this helps
  88.  
  89. Piotr Parlewicz
  90.  
  91.  
  92.